import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { LoadingController, ToastController, AlertController } from 'ionic-angular';

/*
  Generated class for the NotificationProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class NotificationProvider {
  private loading: any;

  constructor(
    public loadingCtrl: LoadingController,
    public toastCtrl: ToastController,
    public alertCtrl: AlertController
  ) {
  }

  /**
   * 弹出加载提示
   * @param msg (可选)
   */
  showLoading(msg?: string) {
    this.loading = this.loadingCtrl.create({
      content: msg || '努力加载中...'
    });
    this.loading.present();
  }

  /**
   * 关闭加载提示
   */
  hideLoading() {
    if (this.loading) {
      this.loading.dismiss();
    }
  }

  /**
   * toast提示，默认顶部，2s
   * @param msg 
   */
  showToast(msg: string) {
    const toast = this.toastCtrl.create({
      message: msg,
      duration: 2000,
      position: 'top'
    });
    toast.present();
  }

  /**
   * toast提示,多参数
   * @param msg 
   * @param position 
   */
  showToastA(msg: string, duration: number, position: string) {
    const toast = this.toastCtrl.create({
      message: msg,
      duration: duration,
      position: position
    });
    toast.present();
  }

  /**
   * alert弹窗
   */
  showAlert(msg: string) {
    const alert = this.alertCtrl.create({
      title: '提示',
      subTitle: msg,
      buttons: ['OK']
    });
    alert.present();
  }

  /**
   * 确认框
   * @param title 
   * @param message 
   * @param btns 
   */
  showConfirm(title: string, message: string, callback, btns?: Array<string>) {
    let text1 = '取消';
    let text2 = '确认';
    if (btns) {
      text1 = btns[0];
      text2 = btns[1];
    }
    const confirm = this.alertCtrl.create({
      title: title,
      message: message,
      buttons: [
        {
          text: text1,

          handler: res => {
            callback(0);
          }
        },
        {
          text: text2,
          handler: res => {
            callback(1);
          }
        }
      ]
    });
    confirm.present();
  }

}
